<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\member\model;

class Growth extends \app\BaseModel
{
    protected $name = "massage_member_growth";
    protected $append = ["type_text"];
    public function getTypeTextAttr($value, $data)
    {
        if(isset($data["type"]) && isset($data["order_id"])) {
            switch ($data["type"]) {
                case 1:
                    $text = "服务订单消费";
                    break;
                case 2:
                    $text = "系统编辑";
                    break;
                case 3:
                    $text = "系统降级";
                    break;
                default:
                    $text = "储值订单消费";
            }
            if(in_array($data["type"], [1, 4])) {
                $model = $data["type"] == 1 ? new \app\massage\model\Order() : new \app\massage\model\BalanceOrder();
                $order_code = $model->where(["id" => $data["order_id"]])->value("order_code");
                $text .= "【订单号:" . $order_code . "】";
            }
            return $text;
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("create_time desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function addRecord($growth, $is_add, $user, $type = 1, $order_id = 0, $create_user = 0)
    {
        $insert = ["uniacid" => $user["uniacid"], "user_id" => $user["id"], "is_add" => $is_add, "growth" => $growth, "order_id" => $order_id, "type" => $type, "create_user" => $create_user, "before_growth" => $user["growth"], "after_growth" => $is_add == 1 ? $user["growth"] + $growth : $user["growth"] - $growth];
        $res = $this->dataAdd($insert);
        $id = $this->getLastInsID();
        $user_model = new \app\massage\model\User();
        if($is_add == 1) {
            $user_model->where(["id" => $user["id"]])->update(["growth" => \think\facade\Db::Raw("growth+" . $growth)]);
        } else {
            $user_model->where(["id" => $user["id"]])->update(["growth" => \think\facade\Db::Raw("growth-" . $growth)]);
        }
        return $id;
    }
}

?>